﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.UI;

/// <summary>
/// Summary description for ImageLib
/// </summary>

namespace LMD.Lib.IO
{
    public static class ImageLib
    {
        /****************************************************************************************
         * NAME     : ResizeImage
         * PURPOSE  : Resize an image
         *
         * REVISIONS
         * No       Date            Version     Author      Description
         * -------  ----------      ----------  ----------  -------------------------------------
         * 1        21/JAN/2011     1.0         IHY         1. Comment Code CREATED
         ****************************************************************************************/
        public static Image ResizeImage(Stream stmInput, int iMaxLargestSide)
        {
            Bitmap bitMap = new Bitmap(stmInput);
            int iOldWidth = bitMap.Width;
            int iOldHeight = bitMap.Height;

            int iNewWidth = iMaxLargestSide;
            int iNewHeight = iMaxLargestSide;

            if (iOldWidth > iMaxLargestSide || iOldHeight > iMaxLargestSide)
            {
                if (iOldWidth >= iOldHeight)
                {
                    iNewWidth = iMaxLargestSide;
                    iNewHeight = (int)Math.Floor(((double)iNewWidth / iOldWidth) * iOldHeight);
                }
                else
                {
                    iNewHeight = iMaxLargestSide;
                    iNewWidth = (int)Math.Floor(((double)iNewHeight / iOldHeight) * iOldWidth);
                }
            }

            Size ThumbNailSize = new Size(iNewWidth, iNewHeight);
            Image oImg = System.Drawing.Image.FromStream(stmInput);
            Image oThumbNail = new Bitmap(ThumbNailSize.Width, ThumbNailSize.Height);

            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle oRectangle = new Rectangle(0, 0, ThumbNailSize.Width, ThumbNailSize.Height);

            oGraphic.DrawImage(oImg, oRectangle);

            return oThumbNail;
            //oThumbNail.Save(ms, frmtImg);
            //byteArray = new byte[ms.Length];
            //ms.Position = 0;
            //ms.Read(byteArray, 0, Convert.ToInt32(ms.Length));

            //oGraphic.Dispose();
            //oImg.Dispose();
            //ms.Close();
            //ms.Dispose();

            //return byteArray;
        }

        public static String ConvertAndSaveImage(Image imgInput, ImageFormat frmtImg, String sxPath, String sxFilenameWithoutExtension)
        { 
            String sxFullFilename = String.Empty;
            String sxExtension = String.Empty;

            sxExtension  = 
                frmtImg.Equals(ImageFormat.Bmp)? ".bmp" :
                frmtImg.Equals(ImageFormat.Emf)? ".emf" :
                frmtImg.Equals(ImageFormat.Exif)? ".exif" :
                frmtImg.Equals(ImageFormat.Gif)? ".gif" :
                frmtImg.Equals(ImageFormat.Icon)? ".ico" :
                frmtImg.Equals(ImageFormat.Jpeg)? ".jpg" :
                frmtImg.Equals(ImageFormat.Png)? ".png" :
                frmtImg.Equals(ImageFormat.Tiff)? ".tif" :
                frmtImg.Equals(ImageFormat.Wmf)? ".wmf" : ".jpg";

            sxFullFilename = sxPath + "\\" + sxFilenameWithoutExtension + sxExtension;

            imgInput.Save(sxFullFilename, frmtImg);

            return sxFilenameWithoutExtension + sxExtension;
        }
    }
}